package com.rainner.admin.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.admin.action.base.EasyuiAction;
import com.rainner.admin.defined.PermissionDefined;
import com.rainner.admin.defined.SystemDefine;
import com.rainner.admin.javabean.ManagerRoleBean;
import com.rainner.admin.javabean.RoleBean;
import com.rainner.admin.server.ManagerRoleServer;
import com.rainner.admin.server.RolePermissionServer;
import com.rainner.admin.server.RoleServer;

@Results({ @Result(name = "ManageOperatorRole", type = "freemarker", location = "/WEB-INF/content/manage/operatorrole.ftl"), @Result(name = "ManageRole", type = "freemarker", location = "/WEB-INF/content/manage/role.ftl") })
public class RoleManageAction extends EasyuiAction {
	private static final long serialVersionUID = 1L;
	private String type;
	private RoleServer server = new RoleServer();
	private RoleBean bean = new RoleBean();
	private String managerName;
	private RolePermissionServer permissionServer = new RolePermissionServer();

	/**
	 * 角色管理界面
	 * 
	 * @return
	 */
	@Action("/manage/role")
	public String manageRole() {
		Object username = ServletActionContext.getRequest().getSession().getAttribute("username");
		if(username != null){
			return "ManageRole";
		}
		return null;
	}

	/**
	 * 角色的增删改查操作
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/manage/role/{type}")
	public String manageRoleType() throws Exception {
		Object username = ServletActionContext.getRequest().getSession().getAttribute("username");
		if (SystemDefine.ROLE_ADD.equals(type)&& permissionServer.checkPermission(PermissionDefined.ADD_ROLE, username.toString())) {
			boolean state = server.manageAddData(bean.getRoleId(), bean.getRoleName());
			if (state) {
				super.setResult(state, "新增角色操作完成.");
			} else {
				super.setResult(state, "新增角色操作失败.");
			}
			map.put("message", result);
		} else if (SystemDefine.ROLE_DELETE.equals(type)&& permissionServer.checkPermission(PermissionDefined.DELETE_ROLE, username.toString())) {
			boolean state = server.manageDelData(bean.getRoleId());
			if (state) {
				super.setResult(state, "删除角色操作完成.");
			} else {
				super.setResult(state, "删除角色操作失败.");
			}
			map.put("message", result);
		} else if (SystemDefine.ROLE_INIT.equals(type)&& permissionServer.checkPermission(PermissionDefined.SEARCH_ROLE, username.toString())) {
			map.put("total", server.manageDataCount());
			map.put("rows", server.manageData(rows, page));
		} else if (SystemDefine.ROLE_UPDATE.equals(type)) {

		}
		return MAP;
	}

	/**
	 * 操作员角色管理界面
	 * 
	 * @return
	 */
	@Action("/manageoperator/role")
	public String manageOperatorRole() {
		Object username = ServletActionContext.getRequest().getSession().getAttribute("username");
		if(username != null){
			return "ManageOperatorRole";
		}
		return null;
	}

	/**
	 * 操作员角色的增删改查
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/manageoperator/role/{type}")
	public String manageOperatorRoleType() throws Exception {
		Object username = ServletActionContext.getRequest().getSession().getAttribute("username");
		ManagerRoleServer managerRoleServer = new ManagerRoleServer();
		if (SystemDefine.OPERATOR_ROLE_INIT.equals(type) && permissionServer.checkPermission(PermissionDefined.SEARCH_MANAGER_ROLE, username.toString())) {
			map.put("total", server.manageDataCount());
			List<RoleBean> role = server.manageData(rows, page);
			List<ManagerRoleBean> managerRole = managerRoleServer.allManagerRoleData(managerName);
			for (RoleBean temp : role) {
				for (ManagerRoleBean managerRoleBean : managerRole) {
					if (temp.getRoleId().equals(managerRoleBean.getRoleId())) {
						temp.setRoleJudge(true);
					}
				}
			}
			map.put("rows", role);
		} else if (SystemDefine.OPERATOR_ROLE_UPDATE.equals(type) && permissionServer.checkPermission(PermissionDefined.UPDATE_MANAGER_ROLE, username.toString())) {
			if (bean.isRoleJudge()) {
				if (!managerRoleServer.addOperatorRole(managerName, bean.getRoleId())) {
					this.result.setState(false);
				}
			} else {
				if (!managerRoleServer.delOperatorRole(managerName, bean.getRoleId())) {
					this.result.setState(false);
				}
			}
			map.put("message", this.result);
		}
		return MAP;
	}

	public void setType(String type) {
		this.type = type;
	}

	public RoleBean getBean() {
		return bean;
	}

	public void setBean(RoleBean bean) {
		this.bean = bean;
	}

	public void setManagerName(String managerName) {
		this.managerName = managerName;
	}

}
